package br.rui.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.rui.dominio.Associacao;
import br.rui.persistencia.ContaDB;

/**
 * Servlet Class
 *
 * @web.servlet              name="Assoc"
 *                           display-name="Name for Assoc"
 *                           description="Description for Assoc"
 * @web.servlet-mapping      url-pattern="/Assoc"
 * @web.servlet-init-param   name="A parameter"
 *                           value="A value"
 */
@SuppressWarnings("serial")
public class Assoc extends HttpServlet {

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException,
		IOException {
		
		ArrayList contasOutros =  new ArrayList();
		ArrayList contasRui = new ArrayList();
		ContaDB contadb = new ContaDB();
						
		String codCliente = "153";
		String[] temp1 = req.getParameterValues("checkoutros");
		
		String temp2 = req.getParameter("radiorui");
		int codRui = Integer.parseInt(temp2.substring(0,temp2.indexOf("#")));
		String descricaorui = temp2.substring(temp2.indexOf("#")+1);
		
		if(temp1!=null){
			for (int i = 0; i < temp1.length; i++) {
				int codFornec = Integer.parseInt(temp1[i].substring(0,temp1[i].indexOf("#")));
				String razaosocial = temp1[i].substring(temp1[i].indexOf("#")+1);
				Associacao associacao = new Associacao(codRui,codFornec,codCliente,razaosocial,descricaorui);
				contadb.CadastraAssociacao(associacao);
			}
		}
		
		contasOutros = (ArrayList) contadb.ConsultarOutros(null);
		contasRui = (ArrayList) contadb.ConsultarPCRui(null);
		
		req.setAttribute("contas",contasRui);
		req.setAttribute("contasOutros",contasOutros);

		RequestDispatcher rd = getServletContext().getRequestDispatcher("/web/jsp/Principal.jsp");
		rd.forward(req,resp);
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException,IOException {
		doGet(req, resp);
	}

}
